<template>
  <div class="user">
    <el-card class="box-card" v-if="status === 'index'">
      <el-row>
        <el-input
          v-model="searchParams.user_id"
          placeholder="请输入主播ID"
          clearable
          style="width:200px"
          size="mini"
        ></el-input>
        <el-input
          v-model="searchParams.id"
            placeholder="请输入红包ID"
          clearable
          style="width:200px"
          size="mini"
        ></el-input>

        <el-select
          v-model="searchParams.type"
          placeholder="请选择类型"
          clearable
          :style="{ width: '150px' }"
          size="mini"
        >
          <el-option
            v-for="item in typeData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
        <el-select
          v-model="searchParams.is_finish"
          placeholder="请选择进度"
          clearable
          :style="{ width: '150px' }"
          size="mini"
        >
          <el-option
            v-for="item in isFinishData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
        <el-button
          type="primary"
          size="mini"
          @click="getData"
          v-if="has('redPackets/index')"
        >搜索
        </el-button
        >
        <el-button
          type="info"
          size="mini"
          @click="reset"
          v-if="has('redPackets/index')"
        >重置
        </el-button
        >
        <el-button
          type="primary"
          @click="add()"
          size="mini"
          v-if="has('redPackets/store')"
        >添加官方红包
        </el-button>
      </el-row>
      <el-table
        :data="datas.data"
        v-loading="table_loading"
        style="width: 100%;margin:10px auto"
        size="mini"
        :header-cell-style="{ background: '#f3f8fe', color: '#333' }"
      >
        <el-table-column
          fixed
          prop="id"
          label="红包信息"
          align="center"
          width="160px"
        >
          <template slot-scope="scope">
            <div style="display: flex;align-items: center;">
              <div>
                <div>{{ scope.row.title }}</div>
                <div>ID:{{ scope.row.id }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="主播信息" align="center" width="160">
          <template slot-scope="scope">
            <div style="display: flex;align-items: center;">
              <img
                :src="scope.row.avatar"
                :style="{ width: '50px', height: '50px', borderRadius: '5px' }"
                @click="bigImage(scope.row.avatar)"
              />
              <div>
                <div>{{ scope.row.nickname }}</div>
                <div>ID:{{ scope.row.user_id }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="exchange_code"
          label="兑换码"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="amount"
          label="红包金额(元)"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="num"
          label="红包个数"
          align="center"
        ></el-table-column>
        <el-table-column prop="is_finish" label="进度" align="center">
          <template slot-scope="scope">
            <span :style="jinduArr[scope.row.is_finish].style"><b>{{ jinduArr[scope.row.is_finish ].title }}</b></span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <el-switch
              v-if="scope.row.is_finish == 0"
              @change="changeStatus(scope.row)"
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="activeValue"
              :inactive-value="inactiveValue"
            >
            </el-switch>
            <div v-else><b>已通过</b></div>
          </template>
        </el-table-column>
        <el-table-column
          prop="created_at"
          label="创建时间"
          align="center"
        ></el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="150">
          <template slot-scope="scope">
<!--            <el-button-->
<!--              type="primary"-->
<!--              size="mini"-->
<!--              @click="detail(scope.row.id)"-->
<!--              v-if="has('redPackets/show')"-->
<!--            >编辑-->
<!--            </el-button-->
<!--            >-->
            <el-button
              type="primary"
              size="mini"
              @click="log(scope.row)"
              v-if="has('redPackets/records')"
            >记录
            </el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="del(scope.row.id, scope.$index)"
              v-if="has('redPackets/destroy')"
            >删除
            </el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        style="text-align:center;margin:20px 0;"
        :current-page="searchParams.page"
        :page-sizes="pageSizes"
        layout="total,prev, pager, next,sizes"
        :total="datas.total"
        @size-change="changeSize"
        @current-change="changePage"
      ></el-pagination>
    </el-card>

    <el-card v-if="status === 'add'">
      <div slot="header">
        <el-page-header @back="back()" content="官方红包添加"></el-page-header>
      </div>
      <el-form
        ref="form"
        :rules="rules"
        :model="editParams"
        label-width="120px"
      >
        <el-form-item label="红包标题" prop="title">
          <el-input v-model="editParams.title" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="红包金额" prop="amount">
          <el-input v-model="editParams.amount" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="红包个数" prop="num">
          <el-input v-model="editParams.num" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="兑换码" prop="dh_code">
          <template>
            <el-select v-model="editParams.dh_code" placeholder="请选择">
              <el-option
                v-for="item in dhCodeArr"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="submit('form', 1)"
            v-if="has('redPackets/store')"
          >保 存
          </el-button
          >
          <el-button type="info" @click="back()" v-if="has('redPackets/store')"
          >取 消
          </el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-show="status === 'log'">
      <div slot="header">
        <el-page-header
          @back="back()"
          @click="getLog(scope.row.id)"
          :content="'领红包记录:红包ID：' + logSearchParams.blind_box_id"
        ></el-page-header>
      </div>
      <el-table
        :data="logData.data"
        v-loading="logTableLoading"
        style="width: 100%;margin:10px auto"
        size="mini"
        :header-cell-style="{ background: '#f3f8fe', color: '#333' }"
      >
        <el-table-column
          prop="id"
          label="ID"
          align="center"
        ></el-table-column>
        <el-table-column
          label="用户信息"
          align="center"
          prop="user_id"
          width="150px"
        >
          <template slot-scope="scope">
            <div style="display: flex;">
              <el-image
                :src="scope.row.avatar"
                :preview-src-list="[scope.row.avatar]"
                fit="contain"
                :style="{
                  width: '50px',
                  height: '50px',
                  borderRadius: '5px',
                  marginRight: '10px',
                  alignItems: 'center'
                }"
              ></el-image>
              <div
                style="display: flex;flex-direction: column;justify-content: space-between;"
              >
                <span>{{ scope.row.nickname }}</span>
                <span>ID:{{ scope.row.receives_user_id }}</span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="amount"
          label="领取金额"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="surplus_amount"
          label="剩余金额"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="created_at"
          label="领取时间"
          align="center"
        ></el-table-column>
      </el-table>
      <el-pagination
        background
        style="text-align:center;margin:20px 0;"
        :current-page="logSearchParams.page"
        :page-sizes="pageSizes"
        layout="total,prev, pager, next,sizes"
        :total="logData.total"
        @size-change="changeLogSize"
        @current-change="changeLogPage"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import api from "../../api/api.js";

export default {
  name: "user",
  components: {
  },
  data() {
    return {
      status: "index", // 页面控制
      datas: {}, // 表格诗句
      levelData: [],
      table_loading: true, // 表格加载中
      loading: false, //全局加载中
      pageSizes: [10, 20, 50, 100, 200, 500, 800, 1000, 2000], // 每页条数默认10
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            }
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            }
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            }
          }
        ]
      },
      statusArr: [
        {id: 0, title: "否", style: {color: "green"}},
        {id: 1, title: "是", style: {color: "red"}}
      ],
      jinduArr: [
        {id: 0, title: "进行中...", style: {color: "green"}},
        {id: 1, title: "已结束", style: {color: "red"}}
      ],
      typeData: [
        {id: 0, name: "主播"},
        {id: 1, name: "官方"},
      ],
      dhCodeArr:[
        {id: 0, name: "否"},
        {id: 1, name: "是"},
      ],
      isFinishData:[
        {id: 0, name: "进行中"},
        {id: 1, name: "已结束"},
      ],
      searchParams: {
        // 搜索参数
        page: 1,
        limit: 10,
        name: ""
      },
      editParams: {
        name: "",
        cat_id: "",
        image: "",
        sort: "",
      },
      rules: {
        // 验证规则
        title: [{required: true, message: "请输入红包标题", trigger: "blur"}],
        amount: [{required: true, message: "请选择红包金额", trigger: "blur"}],
        num: [{required: true, message: "请输入红包个数", trigger: "blur"}],
      },

      activeValue: 1,
      inactiveValue: 0,
      cardStatus: false,
      imageParams: {
        format: "",
        page: 1
      },
      imageData: {},
      imageLoading: false,
      uploadImageUrl: "/admin/common/upload?format=5",
      multiple: false,
      scene: 1, // 打开组件场景
      extra: {},

      prizeData: {data: []},
      prizeSearchParams: {
        wealRoomsPrizesId: "",
        name: "",
        levelId: "",
        page: 1,
        limit: 10
      },
      prizeTableLoading: true, // 赏品表格加载中
      prizeStatus: false,
      submitPrizeData: [],
      selectPrizeData: {data: []},
      addPrizeStatus: false,
      addPrizeData: [],
      editPrizeStatus: false,
      editPrizeParams: {
        id: "",
        goods: {}
      },
      addPrizeLoading: true,
      selectPrizeParams: {levelId: "", page: 1, limit: 20},
      logData: {data: []},
      logSearchParams: {
        // 搜索参数
        userId: "",
        levelId: "",
        page: 1,
        limit: 10,
        blind_box_id: "",
        startTime: "",
        endTime: ""
      },
      logTableLoading: true,
      logTitle: "",
      day: 1,
    };
  },
  created() {
    this.getData();
  },

  methods: {
    showRenwalDialog(e) {
    },
    // 改变图片组件每页条数时
    changeLogSize(limit) {
      this.logSearchParams.limit = limit;
      this.getLog();
    },
    // 改变图片组件当前页码时
    changeLogPage(page) {
      this.logSearchParams.page = page;
      this.getLog();
    },
    log(row) {
      this.logSearchParams.blind_box_id = row.id;
      this.logTitle = row.name;
      this.status = "log";
      this.getLog(row.id);
    },
    getLog(id) {
      this.logTableLoading = true;
      api.redPackets
        .records(this.logSearchParams.blind_box_id, {params: this.logSearchParams})
        .then(res => {
          if (res.data.code == 200) {
            this.logTableLoading = false;
            this.logData = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    changeStatus(e) {
      this.load();
      api.redPackets
        .status(e.id, {status: e.status})
        .then(res => {
          this.unload();
          if (res.data.code === 200) {
            this.$message.success("保存成功");
            this.init();
          }
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 查看图片
    getImages() {
      this.cardStatus = true;
      this.imageLoading = true;
      api.images
        .index({params: this.imageParams})
        .then(res => {
          this.imageLoading = false;
          if (res.data.code == 200) {
            this.imageData = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    closeImage() {
      this.cardStatus = false;
    },
    // 重置图片搜索参数
    initImage(type) {
      this.imageParams.format = type;
      this.imageParams.page = 1;
    },
    // 选择类型筛选图片
    changeImageFormat(type) {
      this.initImage(type);
      this.getImages();
    },
    // 查看大图
    bigImage(url) {
      this.$alert("图片", {
        message: "<img  src=" + url + " style='height:200px;width:200px'>",
        dangerouslyUseHTMLString: true
      });
    },
    back() {
      this.status = "index";
      this.logSearchParams.page = 1;
      this.prizeSearchParams.page = 1;
    },
    // 加载中
    load() {
      this.loading = this.$loading({
        lock: true,
        text: "数据加载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
    },

    // 取消加载
    unload() {
      this.loading.close();
    },
    // 获取所有数据
    getData() {
      this.table_loading = true;
      api.redPackets
        .index({params: this.searchParams})
        .then(res => {
          if (res.data.code === 200) {
            this.table_loading = false;
            this.datas = res.data.data;
            console.log(this.datas)
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 重置搜索参数重新搜索
    reset() {
      this.searchParams.id = "";
      this.searchParams.user_id = "";
      this.searchParams.type = "";
      this.searchParams.is_finish = "";
      this.init();
    },
    submit(formName, type) {
      this.$refs[formName].validate((valid, error) => {
        if (valid) {
          // 添加
          this.load();
          if (type === 1) {
            api.redPackets
              .store(this.editParams)
              .then(res => {
                this.unload();
                if (res.data.code === 200) {
                  this.init();
                }
              })
              .catch(error => {
                console.log(error);
              });
          } else if (type === 2) {
            // 编辑
            api.redPackets
              .edit(this.editParams.id, this.editParams)
              .then(res => {
                this.unload();
                if (res.data.code === 200) {
                  this.init();
                }
              })
              .catch(error => {
                console.log(error);
              });
          }
        } else {
          this.$message.error(Object.values(error)[0][0].message);
          return false;
        }
      });
    },
    delPrize(id, index) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning"
      })
        .then(() => {
          this.load();
          api.wealRoomsPrizes
            .del(id)
            .then(res => {
              this.unload();
              if (res.data.code === 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
                this.prizeData.data.splice(index, 1);
                this.prizeData.total--;
              }
            })
            .catch(error => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    delLog(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning"
      })
        .then(() => {
          this.load();
          api.blindBoxOrderPrizeLogs
            .del(id)
            .then(res => {
              this.unload();
              if (res.data.code === 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
                this.logData.data.splice(index, 1);
                this.logData.total--;
              }
            })
            .catch(error => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 初始化
    init() {
      this.editParams = {
        name: "",
        image: "",
        sort: "",
        price: "",
        three_price: "",
        five_price: "",
        cat_id: "",
        probability: [
          {name: "尊贵", level_id: 1},
          {name: "稀有", level_id: 2},
          {name: "史诗", level_id: 3},
          {name: "传说", level_id: 4}
        ]
      };
      this.back();
      this.getData();
    },
    // 改变每页条数时
    changeSize(limit) {
      this.searchParams.limit = limit;
      this.getData();
    },
    // 改变当前页码时
    changePage(page) {
      this.searchParams.page = page;
      this.getData();
    },
    add() {
      this.status = "add";
    },
    // 添加页面
    detail(id) {
      this.load();
      api.wealRoom
        .show(id)
        .then(res => {
          if (res.data.code === 200) {
            this.unload();
            this.status = "edit";
            this.editParams = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    }
  }
};
</script>

<style scoped lang="less">
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
